<!-- loginPage -->
<template>
  <div class='login'>
    <div
      class="verify"
      v-show="!msg"
    >
      <slide-verify
      ref="slideblock"
      v-loading="loading2"
        :l="42"
        :r="10"
        :w="310"
        :h="155"
        slider-text="向右滑动"
        @success="onSuccess"
        @fail="onFail"
        @refresh="onRefresh"
        @fulfilled="noFulfilled"
      ></slide-verify>
    </div>
    <div class="box" v-show="msg">
    <el-card class="login-form-layout">
      <el-form
        autoComplete="on"
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        label-position="left"
      >
        <div style="text-align: center">
          <i
            class="el-icon-eleme"
            style="width: 56px;height: 56px;color: #409EFF;font-size:50px"
          ></i>
        </div>
        <h2
          class="login-title color-main"
          style="color:#409EFF"
        >登录</h2>
        <el-form-item prop="username">
          <el-input
            name="username"
            type="text"
            v-model="loginForm.username"
            autoComplete="on"
            placeholder="请输入用户名"
          >
            <span slot="prefix">
              <i class="el-icon-user-solid"></i>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :type="pwdType"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password"
            autoComplete="on"
            placeholder="请输入密码"
          >
            <span slot="prefix">
              <i class="el-icon-lock"></i>
            </span>
            <span
              slot="suffix"
              @click="showPwd"
            >
              <i class="el-icon-view"></i>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 60px;text-align: center">
          <el-button
            style="width: 45%"
            type="primary"
            :loading="loading"
            @click.native.prevent="handleLogin"
          >
            登录
          </el-button>
          <el-button
            style="width: 45%"
            type="primary"
            @click.native.prevent="linkToRegister"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
import { isvalidUsername } from '@/utils/validate';
import { UPDATE_USER_INFO } from "../../vuex/mutationsType";
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能小于3位'))
      } else {
        callback()
      }
    };
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'password',
       msg: '未验证',
       loading2:false
    }
  },
  methods: {
    onSuccess() {
      this.msg = 'login success';
      var username = this.loginForm.username;
          var password = this.loginForm.password;
          this.loading = true;
         var lasttiem=new Date();
          /* 登录接口 */
          this.$axios
            .post("http://49.233.65.169:3000/api/login", {
              account: username,
              password,
              lasttiem
            })
            .then(res => {
              if (res.code === 0) {
                this.$message({
                  showClose: true,
                  message: '登录成功！',
                  type: 'success'
                });    
                this.loading = false;
                this.$store.commit(UPDATE_USER_INFO, res.data)       
                this.$router.push('/home'); // 跳转回首页
              } else {
                this.$message({
                  showClose: true,
                  message: '登录出错，请重试',
                  type: 'error'
                });
                this.loading = false;
                 this.$refs.slideblock.reset();
              }
            })
            .catch(err => err);
    },
    onFail() {
       this.$message({
              showClose: true,
              message: '验证失败',
              type: 'error'
            });
      this.msg = ''
       this.loading2=true
    },
    onRefresh() {
      this.msg = ''
    },
    noFulfilled(){
      this.loading2=false
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.msg = "";
        } else {
          console.log('参数验证不合法！');
          return false
        }
      })
    },
    linkToRegister() {
      this.$message({
        showClose: true,
        message: '跳转到注册界面',
        type: 'success'
      });
      this.$router.push('/register')
    }

  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合

  //生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() { }, //生命周期 - 创建之前
  beforeMount() { }, //生命周期 - 挂载之前
  beforeUpdate() { }, //生命周期 - 更新之前
  updated() { }, //生命周期 - 更新之后
  beforeDestroy() { }, //生命周期 - 销毁之前
  destroyed() { }, //生命周期 - 销毁完成
  activated() { }, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style  scoped>
.login {
  background: url("https://www.battlenet.com.cn/login/static/images/login/background/login-background-1280.4JLnG.jpg");
  height: 100vh;
}
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto 0;
  border-top: 10px solid #409eff;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #409eff;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
i {
  color: #409eff;
}
.verify {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -155px;
  margin-top: -104px;
  z-index: 999;
  background-color:#ffffff;
}
</style>